import 'dart:collection';

import 'package:fixnum/fixnum.dart' as $fixnum;
import 'package:flutter/material.dart';

import '../../interface/chat_room_leaf_interface.dart';
import 'mic_resource_list_page.dart';

class MicResourceSettingPageModel extends ChatRoomChangeNotifier {
  // Channel? get channel => roomModel.channel;

  // MicResourceConfigData? data;
  // Map<int, MicSettingItem> get changedItemsMap => roomModel.changedItemsMap;
  // Map<int, MicResourceConfigItem> get configMap =>
  //     roomModel.changedItemConfigMap;

  @override
  onRootProxyModelCreated() {
    // roomModel.changedItemsMap = HashMap();
    // roomModel.changedItemConfigMap = HashMap();
    initData();
  }

  openMicSourceListPage(BuildContext context, int position) {
    _openModalBottomSheet(
        context,
        (context) => MicResourceListPage(
              position: position,
              model: this,
            ));
  }

  _openModalBottomSheet(BuildContext context, WidgetBuilder builder) {
    showModalBottomSheet(
      isScrollControlled: true,
      constraints: BoxConstraints(
          maxHeight: MediaQuery.of(context).size.height -
              MediaQuery.of(context).viewPadding.top),
      shape: const RoundedRectangleBorder(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(24),
          topRight: Radius.circular(24),
        ),
      ),
      context: context,
      builder: builder,
    );
  }

  // updateSettingItem(
  //     BuildContext context, int position, MicResourceConfigItem item) {
  //   print('updateSettingItem:$position');
  //   MicSettingItem micSettingItem = MicSettingItem.create();
  //   micSettingItem.position = position;
  //   micSettingItem.resourceId = item.id;
  //   changedItemsMap[position] = micSettingItem;
  //   configMap[position] = item;
  //   notifyListeners();
  //   Navigator.pop(context);
  // }

  initData() async {
    // var result = await RtcApi.instance.getMicResourceConfigData(
    //     MicResourceType.MIC_FRAME, channel?.channelId.toInt() ?? 0);
    // if (result.isSuccess) {
    //   data = result.data;
    //   int position = 0;
    //   for (var broadcaster in channel?.broadcaster ?? []) {
    //     SimpleChannelUserSlot slot = broadcaster as SimpleChannelUserSlot;
    //     if (slot.micFrameUrl.isNotEmpty) {
    //       for (var configItem in data?.configItem ?? []) {
    //         MicResourceConfigItem item = configItem as MicResourceConfigItem;
    //         if (item.url == slot.micFrameUrl) {
    //           RLog.d('hasMicFrameUrl:$position ${slot.micFrameUrl}');
    //           MicSettingItem micSettingItem = MicSettingItem.create();
    //           micSettingItem.position = position;
    //           micSettingItem.resourceId = item.id;
    //           configMap[position] = item;
    //           changedItemsMap[position] = micSettingItem;
    //         }
    //       }
    //     }
    //     position++;
    //   }
    //   notifyListeners();
    // } else {
    //   showToast(T.current!.setup_fail_reason(result.errorMsg ?? ''));
    // }
  }

  clear() {
    // if (channel == null) {
    //   return;
    // }
    // for (int i = 0; i < channel!.broadcaster.length; i++) {
    //   MicSettingItem micSettingItem = MicSettingItem.create();
    //   micSettingItem.position = i;
    //   micSettingItem.resourceId = $fixnum.Int64(0);
    //   changedItemsMap[i] = micSettingItem;

    //   MicResourceConfigItem micResourceConfigItem =
    //       MicResourceConfigItem.create();
    //   configMap[i] = micResourceConfigItem;
    // }
    notifyListeners();
  }

  confirm(BuildContext context) async {
    // var result = await RtcApi.instance.setMicResource(SetMicResourceReq(
    //     channelId: channel?.channelId, settingItem: changedItemsMap.values));
    // if (result.isSuccess) {
    //   Navigator.of(context).pop();
    // } else {
    //   showToast(T.current!.setup_fail_reason(result.errorMsg ?? ''));
    // }
  }

  @override
  observeMessage(int messageId, data) {}
}
